package com.example.a10.controller;

import cn.hutool.extra.servlet.ServletUtil;
import com.example.a10.entity.ResultVo;
import com.example.a10.util.AddressConstants;
import com.example.a10.entity.Document;
import com.example.a10.server.JsonServer;
import com.example.a10.util.DocumentUtil;
import com.example.a10.util.optionJsonUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JsonParser;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Result")
public class ResultController {

    @Autowired
    HttpServletResponse response;
    @Autowired
    JsonServer jsonServer;
    @Autowired
    private JsonParser jsonParser;

    @ApiOperation("返回所有结果文件的名称")
    @GetMapping("/list")
    public List<Document> list(){
        File file = new File(AddressConstants.Result);
        String[] Trainlist = file.list();
        List<Document> documents = new ArrayList<>();
        for (String train:Trainlist){
            Document document = new Document();
            document.setName(train);
            String filesize = DocumentUtil.getFilesize(AddressConstants.Result + train);
            document.setSize(filesize);
            documents.add(document);
        }
        return documents;
    }

    @ApiOperation("接受结果文件名称,下载结果文件")
    @PostMapping("/download")
    public void Download(@RequestBody Document document){
        String fileName = document.getName();
        File file = new File(AddressConstants.Model + fileName);
        //防止中文乱码
        response.setCharacterEncoding("UTF-8");
        ServletUtil.write(response,new File(AddressConstants.Result+fileName));
    }

    @ApiOperation("接受结果文件名称,返回结果文件数据")
    @PostMapping("/resultlist")
    public ResultVo getJsonData(@RequestBody Document document) throws IOException {
        String resultname = document.getName();
        Map<String, Object> stringObjectMap = jsonServer.readJsonFile(resultname);
        Map<String, Integer> data = new HashMap<>();
        int[] arr = new int[6];
        for (Map.Entry<String, Object> entry : stringObjectMap.entrySet()) {  // 遍历 Map，将数据类型转换为 Integer
            Integer value = Integer.valueOf(entry.getValue().toString());
            if (value>=0){
                arr[value]= arr[value]+1;
            }
            data.put(entry.getKey(), value);
        }
        ResultVo resultVo = new ResultVo();
        resultVo.setData(data);
        String optionJson = optionJsonUtil.getOptionJson(arr);
        resultVo.setOptionJson(optionJson);
        resultVo.setArr(arr);
        return resultVo;  // 返回数据
    }

    @ApiOperation("返回结果文件名称，删除模型")
    @DeleteMapping("/delete")
    public String deleteModel(@RequestBody Document document){
        if(document!=null){
            String name = document.getName();
            File file = new File(AddressConstants.Result+name);
            file.delete();
            return "success";
        }
        return "failed";
    }

}